package com.asset.dispoal.formula.service.commom;

import com.asset.dispoal.formula.domain.db.entitiy.GbFormula;
import com.asset.dispoal.formula.domain.db.mapper.GbFormulaMapper;
import com.asset.dispoal.formula.dto.GbFormulaRequest;
import com.asset.dispoal.formula.service.converter.GbFormulaConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

/**
 * @author qr
 * @date 2022/3/7$
 */
@Component
public class UpdateGbFormulaCommand implements BaseCommand<GbFormulaRequest, Result> {


    @Autowired
    private GbFormulaMapper gbFormulaMapper;

    @Override
    public Result execute(GbFormulaRequest gbFormulaRequest) {
        final GbFormula gbFormula = GbFormulaConverter.INSTANCE.reqToDo(gbFormulaRequest);
        if (gbFormula != null && StringUtil.isNotBlank(gbFormula.getFormulaName())) {
            final int i = gbFormulaMapper.queryFormulaByName(gbFormula.getFormulaName(), gbFormula.getId());
            if (i > 0) {
                return Result.faild("类型名称不能重复");
            }
        }
        gbFormulaMapper.update(gbFormula);
        return Result.ok();
    }
}
